//
//  HJOriginalView.swift
//  sinaWeibo
//
//  Created by hjun on 16/3/26.
//  Copyright © 2016年 hjun. All rights reserved.
//

import UIKit
import SnapKit

///原创微博的View
class HJOriginalView: UIView {
    //MARK: 属性
    
    ///底部约束的属性
    
    
    var bottomViewConstraint: Constraint?
    
    ///微博模型
    var status: HJStatus?{
        
        didSet{
            
            ///设置头像
            
            if let imageUrl = status?.user?.profile_image_url{
                
                let url = NSURL(string: imageUrl)
                
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
        }
           
            ///设置名称
            nameLabel.text = status?.user?.screen_name
            ///设置时间
            timeLabel.text = NSDate.dateWithSinaString(status?.created_at ?? "刚刚").sinaDescripiton()
            ///设置来源
            sourceLabel.text = status?.source
            ///会员等级
            
            memberView.image = status?.user?.mbrankImage
            
            ///认证图标
            attestationView.image = status?.user?.verified_typeImage
            
            
            ///微博内容
            contentLabel.text = status?.text
            
            ///微博配图
            
            ///在添加约束之前先把之前的删了
            
            bottomViewConstraint?.uninstall()
            
            if let pictureURLs = status?.picturesURL{
                
                ///有图片需要显示
                pictureView.hidden = false
                
                ///给pictureView赋值
                
                pictureView.picturesURL = pictureURLs
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomViewConstraint = make.bottom.equalTo(pictureView).offset(HJStatusMagin).constraint
                    
                })
                
                
            }else{
                
                ///没图片不需要显示
                pictureView.hidden = true
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewConstraint = make.bottom.equalTo(contentLabel).offset(HJStatusMagin).constraint
                    
                })
                
            }
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //        self.backgroundColor = UIColor.random()
        prepareUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI(){
        
        
        
        ///添加子控件
        
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(attestationView)
        addSubview(contentLabel)
        addSubview(pictureView)
        ///添加约束
        ///用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(HJStatusMagin)
            make.top.equalTo(self).offset(HJStatusMagin)
            make.size.equalTo(CGSize(width: 35, height: 35))
            
        }
        ///用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(iconView.snp_trailing).offset(HJStatusMagin)
            make.top.equalTo(iconView)
        }
        ///时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(HJStatusMagin)
            make.bottom.equalTo(iconView)
        }
        ///来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLabel.snp_trailing).offset(HJStatusMagin)
            make.centerY.equalTo(timeLabel)
            
            
        }
        
        ///会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLabel.snp_trailing).offset(HJStatusMagin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 13, height: 13))
        }
        
        ///认证图标
        attestationView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        ///内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HJStatusMagin)
            make.trailing.equalTo(self).offset(-HJStatusMagin)
            make.top.equalTo(iconView.snp_bottom).offset(HJStatusMagin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom)
            make.leading.equalTo(self).offset(HJStatusMagin)
            make.size.equalTo(CGSize(width: 222, height: 222))
            
        }
        
        
        
        ///原创cell的底部参照contentLable的底部
        self.snp_makeConstraints(closure: { (make) -> Void in
            
            
            self.bottomViewConstraint = make.bottom.equalTo(pictureView).offset(HJStatusMagin).constraint
            
        })
    }
    
    //MARK: 懒加载
    ///头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    ///名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.text = "hahhaa"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(13)
        
        return label
        
    }()
    
    ///时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(11)
        
        return label
        
        
    }()
    
    ///来源
    
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(11)
        
        return label
    }()
    
    ///会员等级
    
    private lazy var memberView: UIImageView = UIImageView()
    
    ///认证图标
    
    private lazy var attestationView: UIImageView = UIImageView()
    ///原创微博内容
    
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        label.numberOfLines = 0
        return label
        
    }()
    ///原创微博图片
    private lazy var pictureView: HJStatusPictureView = HJStatusPictureView()
        
}
